


<!DOCTYPE html>
<html lang="zh-CN">
  <head><meta name="generator" content="Hexo 3.8.0">
    <title>可编辑的 SVG 图标系统 — Vue.js</title>
    <meta charset="utf-8">
    <meta name="description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="alternate" hreflang="x-default" href="https://vuejs.org/v2/cookbook/editable-svg-icons.html">
    <link rel="alternate" hreflang="zh" href="editable-svg-icons.html">
    <link rel="alternate" hreflang="ja" href="https://jp.vuejs.org/v2/cookbook/editable-svg-icons.html">
    <link rel="alternate" hreflang="ru" href="https://ru.vuejs.org/v2/cookbook/editable-svg-icons.html">
    <link rel="alternate" hreflang="ko" href="https://kr.vuejs.org/v2/cookbook/editable-svg-icons.html">
    <link rel="alternate" hreflang="pt-BR" href="https://br.vuejs.org/v2/cookbook/editable-svg-icons.html">
    <link rel="alternate" hreflang="fr" href="https://fr.vuejs.org/v2/cookbook/editable-svg-icons.html">

    <meta property="og:type" content="article">
    <meta property="og:title" content="可编辑的 SVG 图标系统 — Vue.js">
    <meta property="og:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta property="og:image" content="https://cn.vuejs.org//images/logo.png">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="可编辑的 SVG 图标系统 — Vue.js">
    <meta name="twitter:description" content="Vue.js - The Progressive JavaScript Framework">
    <meta name="twitter:image" content="https://cn.vuejs.org/images/logo.png">

    <link rel="apple-touch-icon" sizes="57x57" href="https://cn.vuejs.org/images/icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="https://cn.vuejs.org/images/icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="https://cn.vuejs.org/images/icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="https://cn.vuejs.org/images/icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="https://cn.vuejs.org/images/icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="https://cn.vuejs.org/images/icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="https://cn.vuejs.org/images/icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="https://cn.vuejs.org/images/icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="https://cn.vuejs.org/images/icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="https://cn.vuejs.org/images/icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="https://cn.vuejs.org/images/icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="https://cn.vuejs.org/images/icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="https://cn.vuejs.org/images/icons/favicon-16x16.png">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <link rel="icon" href="https://cn.vuejs.org/images/logo.png" type="image/png">

    <meta name="msapplication-TileColor" content="#4fc08d">
    <meta name="theme-color" content="#4fc08d">

    <meta name="msapplication-config" content="browserconfig.xml">
    <link rel="manifest" href="https://cn.vuejs.org/manifest.json">

    <!-- <link href="https://fonts.googleapis.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin> -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com" rel="preconnect" crossorigin> -->

    <!-- <link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600|Roboto Mono' rel='stylesheet' type='text/css'> -->
    <!-- <link href='//fonts.googleapis.com/css?family=Dosis:500&text=Vue.js' rel='stylesheet' type='text/css'> -->

    <link href="https://code.bdstatic.com/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- main page styles -->
    <link rel="stylesheet" href="https://cn.vuejs.org/css/page.css">

    <!-- this needs to be loaded before guide's inline scripts -->
    <script src="https://cn.vuejs.org/js/vue.js"></script>

    

    <script>
      Vue.config.productionTip = false
      window.PAGE_TYPE = "cookbook"
    </script>

    <!-- ga -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-46852172-3', 'cn.vuejs.org');
      ga('send', 'pageview');
    </script>
  </head>
  <body class="docs">
    <div id="mobile-bar">
      <a class="menu-button"></a>
      <a class="logo" href="https://cn.vuejs.org/"></a>
    </div>
    <div id="header">
  <a id="logo" href="https://cn.vuejs.org/">
    <img src="https://cn.vuejs.org/images/logo.png" alt="vue logo">
    <span>Vue.js</span>
  </a>
  <ul id="nav">
    <li>
  <form id="search-form">
    <input type="text" id="search-query-nav" class="search-query st-default-search-input" aria-label="搜索">
  </form>
</li>
<li class="nav-dropdown-container learn">
  <a class="nav-link current">学习</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>文档</h4></li>
    <li>
      <ul>
        <li><a href="https://cn.vuejs.org/v2/guide/" class="nav-link">教程</a></li>
        <li><a href="https://cn.vuejs.org/v2/api/" class="nav-link">API</a></li>
        <li><a href="https://cn.vuejs.org/v2/style-guide/" class="nav-link">风格指南</a></li>
        <li><a href="https://cn.vuejs.org/v2/examples/" class="nav-link">示例</a></li>
        <li><a href="https://cn.vuejs.org/v2/cookbook/" class="nav-link current">Cookbook</a></li>
      </ul>
    </li>
    <li><h4>视频教程</h4></li>
    <li>
      <ul>
        <li><a href="https://www.vuemastery.com/courses/" class="nav-link" target="_blank" rel="sponsored noopener">Vue Mastery (英文)</a></li>
        <li><a href="https://vueschool.io/?friend=vuejs&utm_source=Vuejs.org&utm_medium=Link&utm_content=Navbar%20Dropdown" class="nav-link" target="_blank" rel="noopener">Vue School (英文)</a></li>
        <li><a href="https://learning.dcloud.io/" class="nav-link" target="_blank" rel="sponsored noopener">DCloud 视频教程</a></li>
      </ul>
    </li>
  </ul>
</li>

<li class="nav-dropdown-container ecosystem">
  <a class="nav-link">生态系统</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><h4>帮助</h4></li>
    <li><ul>
      <li><a href="https://forum.vuejs.org/" class="nav-link" target="_blank" rel="noopener">论坛</a></li>
      <li><a href="https://chat.vuejs.org/" class="nav-link" target="_blank" rel="noopener">聊天室</a></li>
      <li><a href="https://events.vuejs.org/meetups/" class="nav-link" target="_blank" rel="noopener">聚会</a></li>
    </ul></li>
    <li><h4>工具</h4></li>
    <li>
      <ul>
        <li><a href="https://github.com/vuejs/vue-devtools" class="nav-link" target="_blank" rel="noopener">Devtools</a></li>
        <li><a href="https://cli.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue CLI</a></li>
        <li><a href="https://vue-loader.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Loader</a></li>
      </ul>
    </li>
    <li><h4>核心插件</h4></li>
    <li><ul>
      <li><a href="https://router.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue Router</a></li>
      <li><a href="https://vuex.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vuex</a></li>
      <li><a href="https://ssr.vuejs.org/zh/" class="nav-link" target="_blank" rel="noopener">Vue 服务端渲染</a></li>
    </ul></li>
    <li><h4>信息</h4></li>
    <li><ul>
      <li><a href="https://news.vuejs.org" class="nav-link" target="_blank" rel="noopener">周刊</a></li>
      <li><a href="https://github.com/vuejs/vue/projects/6" class="nav-link" target="_blank" rel="noopener">Roadmap</a></li>
      <li><a href="https://events.vuejs.org/" class="nav-link" target="_blank" rel="noopener">活动</a></li>
      <li><a href="https://twitter.com/vuejs" class="nav-link" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://medium.com/the-vue-point" class="nav-link" target="_blank" rel="noopener">博客</a></li>
      <li><a href="https://vuejobs.com/?ref=vuejs" class="nav-link" target="_blank" rel="noopener">工作</a></li>
    </ul></li>
  </ul>
</li>

<li>
  <a href="https://cn.vuejs.org/v2/guide/team.html" class="nav-link team">团队</a>
</li>
<li class="nav-dropdown-container resources">
  <a href="editable-svg-icons.html#" class="nav-link">资源列表</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://cn.vuejs.org/resources/partners.html" class="nav-link">合作伙伴</a></li>
    <li><a href="https://cn.vuejs.org/resources/themes.html" class="nav-link">主题</a></li>
    <li><a href="https://github.com/vuejs/awesome-vue" class="nav-link" target="_blank">Awesome Vue</a></li>
  </ul>
</li>

<!-- <li>
  <a href="/partners" class="nav-link ">Partners</a>
</li> -->
<li class="nav-dropdown-container support-vue">
  <a href="https://cn.vuejs.org/support-vuejs/" class="nav-link">支持 Vue</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://cn.vuejs.org/support-vuejs/#One-time-Donations" class="nav-link">一次性赞助</a></li>
    <li><a href="https://cn.vuejs.org/support-vuejs/#Recurring-Pledges" class="nav-link">周期性赞助</a></li>
    <li><a href="https://www.smallsticker.com/%E8%B4%B4%E7%BA%B8/vue.html" rel="noopener" class="nav-link">贴纸</a></li>
    <li><a href="https://osholic.com/?utm_source=vue&utm_medium=dropdown" rel="noopener" class="nav-link">周边</a></li>
    <li><a href="https://vue.threadless.com" target="_blank" rel="noopener" class="nav-link">T 恤商店</a></li>
  </ul>
</li>

<li class="nav-dropdown-container language">
  <a class="nav-link">多语言</a><span class="arrow"></span>
  <ul class="nav-dropdown">
    <li><a href="https://vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">English</a></li>
    <li><a href="https://jp.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">日本語</a></li>
    <li><a href="https://ru.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">Русский</a></li>
    <li><a href="https://kr.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">한국어</a></li>
    <li><a href="https://br.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">Português</a></li>
    <li><a href="https://fr.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">Français</a></li>
    <li><a href="https://vi.vuejs.org/v2/cookbook/editable-svg-icons.html" class="nav-link" target="_blank" rel="noopener">Tiếng Việt</a></li>
  </ul>
</li>

<li><a href="https://github.com/vuejs/cn.vuejs.org/" target="_blank" class="nav-link contribute">参与翻译</a></li>

  </ul>
</div>

    
      <div id="main" class="fix-sidebar">
        
          
  

<div class="sidebar">
  <div class="sidebar-inner">
    
    <div class="list">
      <div id="sidebar-sponsors-special">
  <div class="main-sponsor">
    <span>特别赞助商</span>
    <div>
    <a href="http://www.dcloud.io/?hmsr=vuejsorg&hmpl=&hmcu=&hmkw=&hmci=" target="_blank" rel="sponsored noopener" class="logo">
      <img src="../../images/dcloud.gif" alt="">
    </a>
    </div>
    <div>
    <a href="https://youzan.github.io/vant?source=vuejsorg" target="_blank" rel="sponsored noopener" class="logo">
      <img src="../../images/vant.png" alt="">
    </a>
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-left">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://www.nativescript.org/vue?utm_source=vue-js-org&utm_medium=website&utm_campaign=nativescript-awareness" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/nativescript.png" alt="NativeScript">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/retool.png" alt="Retool">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/support-vuejs">
    成为赞助者
  </a>
</div>


-->

      
        <h2>
          
          Cookbook<sup class="beta">beta</sup>
          
        </h2>
        <ul class="menu-root">
  
    
    
    <li>
      <a href="https://cn.vuejs.org/v2/cookbook/index.html" class="sidebar-link">介绍</a>
    </li>
  
    
    
    <li>
      <a href="adding-instance-properties.html" class="sidebar-link">添加实例属性</a>
    </li>
  
    
    
    <li>
      <a href="form-validation.html" class="sidebar-link">表单校验</a>
    </li>
  
    
    
    <li>
      <a href="editable-svg-icons.html" class="sidebar-link current">可编辑的 SVG 图标系统</a>
    </li>
  
    
    
    <li>
      <a href="serverless-blog.html" class="sidebar-link">创建一个基于 CMS 的博客</a>
    </li>
  
    
    
    <li>
      <a href="unit-testing-vue-components.html" class="sidebar-link">Vue 组件的单元测试</a>
    </li>
  
    
    
    <li>
      <a href="creating-custom-scroll-directives.html" class="sidebar-link">创建自定义滚动指令</a>
    </li>
  
    
    
    <li>
      <a href="debugging-in-vscode.html" class="sidebar-link">在 VS Code 中调试</a>
    </li>
  
    
    
    <li>
      <a href="using-axios-to-consume-apis.html" class="sidebar-link">使用 axios 访问 API</a>
    </li>
  
    
    
    <li>
      <a href="avoiding-memory-leaks.html" class="sidebar-link">避免内存泄漏</a>
    </li>
  
    
    
    <li>
      <a href="client-side-storage.html" class="sidebar-link">客户端存储</a>
    </li>
  
    
    
    <li>
      <a href="packaging-sfc-for-npm.html" class="sidebar-link">Packaging Vue Components for npm</a>
    </li>
  
    
    
    <li>
      <a href="dockerize-vuejs-app.html" class="sidebar-link">Dockerize Vue.js App</a>
    </li>
  
    
    
    <li>
      <a href="practical-use-of-scoped-slots.html" class="sidebar-link">Practical use of scoped slots with GoogleMaps</a>
    </li>
  
</ul>

      
    </div>
  </div>
</div>
<!--

<div id="sidebar-sponsors-platinum-right">
  <div class="main-sponsor">
    <span>白金赞助商</span>
    <div>
    <a href="https://vueschool.io/?utm_source=Vuejs.org&utm_medium=Banner&utm_campaign=Sponsored%20Banner&utm_content=V1" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vueschool.png" alt="VueSchool">
    </a>
    <a href="https://vehikl.com/" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/vehikl.png" alt="Vehikl">
    </a>
    <a href="https://www.nativescript.org/vue?utm_source=vue-js-org&utm_medium=website&utm_campaign=nativescript-awareness" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/nativescript.png" alt="NativeScript">
    </a>
    <a href="https://retool.com/?utm_source=sponsor&utm_campaign=vue" target="_blank" rel="sponsored noopener" class="logo">
      <img src="/images/retool.png" alt="Retool">
    </a>
    </div>
  </div>
  <a class="become-backer" href="/support-vuejs">
    成为赞助者
  </a>
</div>


-->


<div class="content cookbook with-sidebar ">
  
    
      
<div id="ad">
  <span style="float: right;">广告</span>
  <a href="https://time.geekbang.org/column/intro/216?code=mM%2FdqOPks0VQ7EkYGPNisoEpHDsb0ViHBXYa%2Fyg02k8%3D&utm_term=SPoster" target="_blank" rel="sponsored noopener">
    <img src="../../images/geekbang-ad.jpg">
  </a>
  <!-- <a href="https://ke.qq.com/course/392130?saleToken=992600&from=pclink" target="_blank" rel="sponsored noopener">
    <img src="/images/tencent-ad.png">
  </a> -->
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CKYIK2QU&placement=vuejsorg"
    id="_carbonads_js">
  </script> -->
</div>


    
  
  
    <h1>可编辑的 SVG 图标系统</h1>
  

  <div class="ad-pagetop">
  <a href="http://www.tp-shop.cn/index.php?http_referer=vuejs" target="_blank" rel="noopener noreferrer">
    <span class="name">TPshop</span>
    <span class="description">中国免费商城系统 - 搜豹商城系统 - 免费50小时Vue视频教程</span>
    <span class="action">立即查看 &gt;</span>
  </a>
</div>


  
    <h2 id="基本的示例"><a href="editable-svg-icons.html#基本的示例" class="headerlink" title="基本的示例"></a>基本的示例</h2><p>创建一套 SVG 图标系统的方式有很多，但是有一种方法能够充分发挥 Vue 的能力，那就是把可编辑的内联图标创建为组件。这样做有好多好处：</p>
<ul>
<li>图标易于实时修改</li>
<li>图标可以带动画</li>
<li>你可以使用标准的 prop 和默认值来将图标保持在一个典型的尺寸并随时按需改变它们</li>
<li>图标是内联的，所以不需要额外的 HTTP 请求</li>
<li>可以动态地使得图标可访问</li>
</ul>
<p>首先，我们将为所有的图标创建一个文件夹，并将这些图标以一种标准化的方式命名以便获取它们：</p>
<blockquote>
<p><code>components/icons/IconBox.vue</code><br><code>components/icons/IconCalendar.vue</code><br><code>components/icons/IconEnvelope.vue</code></p>
</blockquote>
<p>这里有一个示例的仓库，你可以看到全部的配置：<a href="https://github.com/sdras/vue-sample-svg-icons/" target="_blank" rel="noopener">https://github.com/sdras/vue-sample-svg-icons/</a>。</p>
<p><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/28963/screendocs.jpg" alt="网站文档" title="文档 demo"></p>
<p>我们将会创建一个基础图标 (<code>IconBase.vue</code>) 组件，并使用了一个插槽。</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
    <span class="hljs-attr">:width</span>=<span class="hljs-string">"width"</span>
    <span class="hljs-attr">:height</span>=<span class="hljs-string">"height"</span>
    <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 18 18"</span>
    <span class="hljs-attr">:aria-labelledby</span>=<span class="hljs-string">"iconName"</span>
    <span class="hljs-attr">role</span>=<span class="hljs-string">"presentation"</span>
  &gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>
      <span class="hljs-attr">:id</span>=<span class="hljs-string">"iconName"</span>
      <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>
    &gt;</span>&#123;&#123; iconName &#125;&#125; icon<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">:fill</span>=<span class="hljs-string">"iconColor"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></code></pre>
<p>你可以像这样使用这个基础图标，唯一可能要做的就是根据你图标的 <code>viewBox</code> 来更新其 <code>viewBox</code>。在基础图标里会有 <code>width</code>、<code>height</code>、<code>iconColor</code> 以及图标的名字等 prop，这样我们就可以通过 prop 对其动态更新。这个名字将会同时用在 <code>&lt;title&gt;</code> 的内容及其用于提供可访问性的 <code>id</code> 上。</p>
<p>我们的脚本是下面这样的，我们设了一些默认值，这样在没有特别设置的情况下图标渲染出来就是一致的：</p>
<pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
  <span class="hljs-attr">props</span>: &#123;
    <span class="hljs-attr">iconName</span>: &#123;
      <span class="hljs-attr">type</span>: <span class="hljs-built_in">String</span>,
      <span class="hljs-attr">default</span>: <span class="hljs-string">'box'</span>
    &#125;,
    <span class="hljs-attr">width</span>: &#123;
      <span class="hljs-attr">type</span>: [<span class="hljs-built_in">Number</span>, <span class="hljs-built_in">String</span>],
      <span class="hljs-attr">default</span>: <span class="hljs-number">18</span>
    &#125;,
    <span class="hljs-attr">height</span>: &#123;
      <span class="hljs-attr">type</span>: [<span class="hljs-built_in">Number</span>, <span class="hljs-built_in">String</span>],
      <span class="hljs-attr">default</span>: <span class="hljs-number">18</span>
    &#125;,
    <span class="hljs-attr">iconColor</span>: &#123;
      <span class="hljs-attr">type</span>: <span class="hljs-built_in">String</span>,
      <span class="hljs-attr">default</span>: <span class="hljs-string">'currentColor'</span>
    &#125;
  &#125;
&#125;</code></pre>
<p><code>currentColor</code> 会成为 <code>fill</code> 的默认值，于是图标就会继承周围文字的颜色了。我们也可以根据需求传递一个不一样的颜色的 prop。</p>
<p>我们可以这样使用它，通过 <code>IconWrite.vue</code> 将图标的路径包含于其中，作为其唯一的内容：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">icon-base</span> <span class="hljs-attr">icon-name</span>=<span class="hljs-string">"write"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">icon-write</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">icon-base</span>&gt;</span></code></pre>
<p>现在，如果我们想要创建更多种尺寸的图标，我们可以简单的这样做：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- 你可以通过 prop 传递更小的 `width` 和 `height` --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">icon-base</span>
    <span class="hljs-attr">width</span>=<span class="hljs-string">"12"</span>
    <span class="hljs-attr">height</span>=<span class="hljs-string">"12"</span>
    <span class="hljs-attr">icon-name</span>=<span class="hljs-string">"write"</span>
  &gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">icon-write</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">icon-base</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- 或者你可以使用默认值，即 18 --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">icon-base</span> <span class="hljs-attr">icon-name</span>=<span class="hljs-string">"write"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">icon-write</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">icon-base</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- 或者让它更大一些 :) --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">icon-base</span>
    <span class="hljs-attr">width</span>=<span class="hljs-string">"30"</span>
    <span class="hljs-attr">height</span>=<span class="hljs-string">"30"</span>
    <span class="hljs-attr">icon-name</span>=<span class="hljs-string">"write"</span>
  &gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">icon-write</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">icon-base</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></code></pre>
<p><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/28963/Screen%20Shot%202018-01-01%20at%204.51.40%20PM.png" width="450"></p>
<h2 id="带动画的图标"><a href="editable-svg-icons.html#带动画的图标" class="headerlink" title="带动画的图标"></a>带动画的图标</h2><p>当我们想要赋予它们动画，尤其是在一个交互动作的时候，在组件中控制图标是非常方便的。内联 SVG 对各种交互行为都有最高级的支持。这里有一个图标在点击之后产生动画的基本的示例：</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">svg</span>
    @<span class="hljs-attr">click</span>=<span class="hljs-string">"startScissors"</span>
    <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/2000/svg"</span>
    <span class="hljs-attr">viewBox</span>=<span class="hljs-string">"0 0 100 100"</span>
    <span class="hljs-attr">width</span>=<span class="hljs-string">"100"</span>
    <span class="hljs-attr">height</span>=<span class="hljs-string">"100"</span>
    <span class="hljs-attr">aria-labelledby</span>=<span class="hljs-string">"scissors"</span>
    <span class="hljs-attr">role</span>=<span class="hljs-string">"presentation"</span>
  &gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"scissors"</span>
      <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>
    &gt;</span>Scissors Animated Icon<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">path</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"bk"</span>
      <span class="hljs-attr">fill</span>=<span class="hljs-string">"#fff"</span>
      <span class="hljs-attr">d</span>=<span class="hljs-string">"M0 0h100v100H0z"</span>/&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"leftscissor"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M..."</span>/&gt;</span>
      ...
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">g</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"rightscissor"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">path</span> <span class="hljs-attr">d</span>=<span class="hljs-string">"M..."</span>/&gt;</span>
      ...
    <span class="hljs-tag">&lt;/<span class="hljs-name">g</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">svg</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span></code></pre>
<pre><code class="hljs js"><span class="hljs-keyword">import</span> &#123; TweenMax, Sine &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'gsap'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;
  <span class="hljs-attr">methods</span>: &#123;
    startScissors() &#123;
      <span class="hljs-keyword">this</span>.scissorAnim(<span class="hljs-keyword">this</span>.$refs.rightscissor, <span class="hljs-number">30</span>)
      <span class="hljs-keyword">this</span>.scissorAnim(<span class="hljs-keyword">this</span>.$refs.leftscissor, <span class="hljs-number">-30</span>)
    &#125;,
    scissorAnim(el, rot) &#123;
      TweenMax.to(el, <span class="hljs-number">0.25</span>, &#123;
        <span class="hljs-attr">rotation</span>: rot,
        <span class="hljs-attr">repeat</span>: <span class="hljs-number">3</span>,
        <span class="hljs-attr">yoyo</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">svgOrigin</span>: <span class="hljs-string">'50 45'</span>,
        <span class="hljs-attr">ease</span>: Sine.easeInOut
      &#125;)
    &#125;
  &#125;
&#125;</code></pre>
<p>我们通过 <code>ref</code> 将我们需要移动的路径做了分组，因为这把剪刀的两侧需要同时移动，所以我们会创建一个传入 <code>ref</code> 的可复用的函数。并使用 GreenSock 帮助我们完成动画并解决 <code>transform-origin</code> 的浏览器兼容性问题。</p>
<p data-height="300" data-theme-id="0" data-slug-hash="dJRpgY" data-default-tab="result" data-user="Vue" data-embed-version="2" data-pen-title="Editable SVG Icon System: Animated icon" class="codepen">在 <a href="https://codepen.io" target="_blank" rel="noopener">CodePen</a> 查看 Vue (<a href="https://codepen.io/Vue" target="_blank" rel="noopener">@Vue</a>) 的 <a href="https://codepen.io/team/Vue/pen/dJRpgY/" target="_blank" rel="noopener">可编辑的 SVG 图标系统：带动画的图标</a>。</p><br><script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script><br><br><p style="margin-top:-30px">很容易做到吧！并且很容易实时更新。</p>

<p>你可以在<a href="https://github.com/sdras/vue-sample-svg-icons/" target="_blank" rel="noopener">这个仓库</a>看到更多动画的示例。</p>
<h2 id="注意事项"><a href="editable-svg-icons.html#注意事项" class="headerlink" title="注意事项"></a>注意事项</h2><p>设计师可能会改变他们的主意。产品需求也可能会变更。在一个基础组件中保持整个图标系统的逻辑，意味着你可以快速更新你所有的图标并快速丰富你的整个图标系统。甚至通过图标加载器，有些情况下需要你重新创建或编辑每个 SVG 来完成全局的改动。这个方法可以节约你的时间，减少你的痛苦。</p>
<h2 id="何时避免使用这个模式"><a href="editable-svg-icons.html#何时避免使用这个模式" class="headerlink" title="何时避免使用这个模式"></a>何时避免使用这个模式</h2><p>当你有许多图标用在你网站不同的地方时，这类 SVG 图标系统是非常有用的。如果你只在一个页面上重复使用相同的图标很多次 (比如一个大表格中每行的删除图标)，可能把它们编译到一个雪碧图表并通过 <code>&lt;use&gt;</code> 标签来加载会更好。</p>
<h2 id="其它替代方案"><a href="editable-svg-icons.html#其它替代方案" class="headerlink" title="其它替代方案"></a>其它替代方案</h2><p>其它帮助你管理 SVG 图标的工具有：</p>
<ul>
<li><a href="https://github.com/kisenka/svg-sprite-loader" target="_blank" rel="noopener">svg-sprite-loader</a></li>
<li><a href="https://github.com/rpominov/svgo-loader" target="_blank" rel="noopener">svgo-loader</a></li>
</ul>
<p>这些工具会在编译时打包 SVG，但是在运行时编辑它们会有一些麻烦，因为 <code>&lt;use&gt;</code> 标签在处理一些复杂的事情时存在浏览器兼容问题。同时它们会给你两个嵌套的 <code>viewBox</code> 属性，这是两套坐标系。所以实现上稍微复杂了一些。</p>

  
  
  <div class="footer">
      <script src="https://m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div id="bsa-native"></div>
<script>
_bsa.init('custom', 'CKYD62QM', 'placement:vuejsorg',
  {
    target: '#bsa-native',
    template: '<a class="native-box" href="editable-svg-icons.html##statlink##"><div class="native-sponsor">Sponsor</div><div class="native-text"><strong>##company##</strong> — ##description##</div></a>'
  }
);
</script>


    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/cookbook/editable-svg-icons.md" rel="noopener" target="_blank">
      在 GitHub 上编辑此页！
    </a>
  </div>
</div>

        
      </div>
      <script src="https://cn.vuejs.org/js/smooth-scroll.min.js"></script>
    

    <!-- main custom script for sidebars, version selects etc. -->
    <script src="https://cn.vuejs.org/js/css.escape.js"></script>
    <script src="https://cn.vuejs.org/js/common.js"></script>

    <!-- search -->
    <link href="https://code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cn.vuejs.org/css/search.css">
    <script src="https://code.bdstatic.com/npm/docsearch.js@1.5.0/dist/cdn/docsearch.min.js"></script>
    <script>
    [
      '#search-query-nav',
      '#search-query-sidebar',
      '#search-query-menu'
    ].forEach(function (selector) {
      if (!document.querySelector(selector)) return
      // search index defaults to v2
      var match = window.location.pathname.match(/^\/(v\d+)/)
      var version = match ? match[1] : 'v2'
      docsearch({
      appId: 'BH4D9OD16A',
      apiKey: '5638280abff9d207417bb03be05f0b25',
      indexName: 'vuejs_cn2',
      inputSelector: selector,
      algoliaOptions: { facetFilters: ["version:" + version] },
      autocompleteOptions: { hint: false, appendTo: 'body'}
      })
    })
    </script>
  </body>
</html>
